class Snake{
  constructor(options){
    //蛇的大小
    this.width = options.width || 20;
    this.height = options.height || 20;
    //蛇的身体
    this.body = [
      {x : 3, y : 2, color : "red"},
      {x : 2, y : 2, color : "blue"},
      {x : 1, y : 2, color : "blue"},
    ];
    //移动方向
    this.direction = "right";
    this.element = [];
  }
  //创建蛇
  create(box){
    //生成蛇的身体
    for(var i = 0, len = this.body.length; i < len; i++){
      //确认每一个身体
      var price = this.body[i];
      //创建并把每个身体渲染出来
      var ele = document.createElement("div");
      ele.style.width = this.width + "px";
      ele.style.height = this.height + "px";
      ele.style.left = price.x * this.width + "px";
      ele.style.top = price.y * this.height + "px";
      ele.style.backgroundColor = price.color;
      ele.style.position = "absolute";

      //添加
      box.appendChild(ele);

      this.element.push(ele);
    }
  }
  //蛇的运动
  run(){
    for(var i = this.body.length - 1; i > 0; i--){
      this.body[i].x = this.body[i-1].x;
      this.body[i].y = this.body[i-1].y;
    }
    var head = this.body[0];
    switch(this.direction){
      case "right":
        head.x++;
        break;
      case "left":
        head.x--;
        break;
      case "top":
        head.y--;
        break;
      case "bottom":
        head.y++;
    }
  }
  //蛇的删除
  remove(box){
    for(var i = this.element.length-1; i >= 0; i--){
      box.removeChild(this.element[i])
    }
    this.element = [];
  }
}
